package com.software.frontend.mine.adapters;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.software.frontend.R;
import com.software.frontend.mine.entity.Like;
import com.software.frontend.mine.utils.PictureProcessUtils;

import java.util.List;

public class LikesAdapter extends BaseAdapter {

    LayoutInflater inflater;
    List<Like> data;

    public LikesAdapter(Context context, List data) {
        inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Like like = data.get(i);
        //需要判断传入的Likes是否合法
        List<Bitmap> ohtersHeads = like.getOhtersHead();
        if (like.getPostPicture() != null && ohtersHeads != null && ohtersHeads.size()>0) {
            if (ohtersHeads.size() == 1){
                //单人点赞
                view = (ConstraintLayout)inflater.inflate(R.layout.item_notices_likes_one, viewGroup,false);
                if (like.getSelfName() != null && !like.getSelfName().equals("") && like.getTimeLate() >= 0){
                    TextView name = (TextView) view.findViewById(R.id.textView7);
                    name.setText("@" + like.getSelfName());
                    name.setVisibility(View.VISIBLE);
                    TextView timeLate = (TextView) view.findViewById(R.id.textView8);
                    if (like.getTimeLate() == 0){
                        timeLate.setText("刚刚");
                    }else {
                        timeLate.setText(like.getTimeLate() + "分钟前");
                    }
                    timeLate.setVisibility(View.VISIBLE);
                    ImageView selfHead = (ImageView)view.findViewById(R.id.imageView);
                    selfHead.setImageBitmap(PictureProcessUtils.toRoundBitmap(ohtersHeads.get(0)));
                }else {
                    errorHandle();
                }
            }else {
                //多人点赞
                view = (ConstraintLayout)inflater.inflate(R.layout.item_notices_likes_much, viewGroup,false);
                if (ohtersHeads.size() > 0){
                    ImageView omit = (ImageView) view.findViewById(R.id.imageView11);
                    omit.setVisibility(View.VISIBLE);
                    ImageView[] imageViews = new ImageView[5];
                    imageViews[0] = view.findViewById(R.id.imageView5);
                    imageViews[1] = view.findViewById(R.id.imageView6);
                    imageViews[2] = view.findViewById(R.id.imageView7);
                    imageViews[3] = view.findViewById(R.id.imageView8);
                    imageViews[4] = view.findViewById(R.id.imageView9);
                    if (ohtersHeads.size() > 5){
                        for (int j = 0; j < 5; j++){
                            imageViews[j].setVisibility(View.VISIBLE);
                            imageViews[j].setImageBitmap(PictureProcessUtils.toRoundBitmap(ohtersHeads.get(j)));
                        }
                    }else {
                        for (int j = 0; i < ohtersHeads.size()-1; j++){
                            imageViews[j].setVisibility(View.VISIBLE);
                            imageViews[j].setImageBitmap(PictureProcessUtils.toRoundBitmap(ohtersHeads.get(j)));
                        }
                    }
                }else {
                    errorHandle();
                }
            }
            TextView count = (TextView)view.findViewById(R.id.textView9);
            if (ohtersHeads.size() != 1){
                count.setText("等" + ohtersHeads.size() + "人点赞了您的视频");
            }else {
                count.setText("点赞了您的视频");
            }
            ImageView postPicture = (ImageView)view.findViewById(R.id.imageView10);
            postPicture.setImageBitmap(like.getPostPicture());
        }else {
            errorHandle();
        }

        return view;
    }

    private void errorHandle() {
        //错误处理
    }
}
